
local ZoneView = import(".ZoneView")


local LoginView = class("LoginView",function()
    local layer = display.newLayer()
    layer:setName("LoginView")
    return layer
end)

function LoginView:ctor()
    -- menu
    local loginMenu, bindingNodes = cc.CSLoader:createNode(Res.LoginMenu,true, true, true)
    display.doLayout(loginMenu)
    self:addChild(loginMenu)
    loginMenu:setVisible(false)

    self.mLoginMenu = loginMenu

    self.versionLabel = bindingNodes.versionLabel
    self._serverBtn = loginMenu:findChild("Panel/Button_server")
    self._curZoneLabel = self._serverBtn:getChildByName("name")

    self._serverBtn:addClickEventListener(function(sender)
        executeFunc(self._onOperate, "openZoneList")
    end)

    bindingNodes.enterGameBtn:addClickEventListener(function(sender) 
        executeFunc(self._onOperate, "enterGame")
    end)
    bindingNodes.switchAccount:addClickEventListener(function(sender) 
        executeFunc(self._onOperate, "switchAccount")
    end)

    -- 修复功能暂时关掉
    bindingNodes.service:setPositionY(bindingNodes.switchAccount:getPositionY())
    bindingNodes.switchAccount:setPositionY(bindingNodes.fixBtn:getPositionY())
    bindingNodes.fixBtn:setVisible(false)
    -- bindingNodes.fixBtn:addClickEventListener(function(sender) 
    --     local confirmDialog = display.newConfirmDialog(L("repair_text"),
    --         function()
    --             app:fixClient()
    --         end)
    --     UIHandler:addPopDialog(confirmDialog)
    -- end)

    bindingNodes.announcement:addClickEventListener(function(sender) 
        executeFunc(self._onOperate, "openNotice")
    end)
    bindingNodes.service:addClickEventListener(function(sender) -- 客服按钮
        executeFunc(self._onOperate, "openLink", "https://kf.qq.com/touch/scene_product.html?scene_id=kf6453")
    end)
    local isAgree = app:getValue(Enums.UserDefaultKey.TencentAgreement, false)
    local choosePanel = loginMenu:findChild("Panel/Panel_choose")
    
    local qqBtn1 = choosePanel:getChildByName("name_btn_1")
    local qqBtn2 = choosePanel:getChildByName("name_btn_2")
    if app:getInnerPublisher() == Enums.Publisher.tencent then
        qqBtn1:setTouchEnabled(true)
        qqBtn2:setTouchEnabled(true)
        qqBtn1:addClickEventListener(function()
            -- 腾讯游戏许可及服务协议
            executeFunc(self._onOperate, "openLink", "https://game.qq.com/contract.shtml")
        end)
        qqBtn2:addClickEventListener(function()
            -- 隐私政策
            executeFunc(self._onOperate, "openLink", "http://www.tencent.com/zh-cn/zc/privacypolicyTraditional.shtml")
        end)
        bindingNodes.service:setVisible(true)
    else
        bindingNodes.service:setVisible(false)
        choosePanel:setVisible(false)
        isAgree = true
    end

    
    self._readBtn = choosePanel:getChildByName("selected_btn")
    self._readBtn:setBright(isAgree)
    display.uiAddClick(choosePanel, function()
        local isBright = self._readBtn:isBright()
        app:saveValue(Enums.UserDefaultKey.TencentAgreement, not isBright)
        self._readBtn:setBright(not isBright)
    end)

   
    if app:isEma() then
        local ZoneView = require("app.login.views.ZoneViewEma")
        self._zoneView = ZoneView.new(app.mvc.VIEWTYPE_BLUR)
    else
        local ZoneView = require("app.login.views.ZoneView")
        self._zoneView = ZoneView.new(app.mvc.VIEWTYPE_BLUR)
    end
    
   
    -- ZoneView
    self._zoneView:setVisible(false)
    self:addChild(self._zoneView)
    self._zoneView:onOperate(function(op, data)
        if op == "exit" then
            if self.mOpenListPlace == 1 then
                self:serverListVisibility(false)
                executeFunc(self._onOperate, "backFromZoneList")
            else
                executeFunc(self._onOperate, "enterGame")
            end
        elseif op == "selectList" then
            executeFunc(self._onOperate, "selectPage", data.tag)
        elseif op == "selectZone" then
            executeFunc(self._onOperate, "selectZone", data.id)
            executeFunc(self._onOperate, "enterGame")
        elseif op == "selectZoneByTag" then
            executeFunc(self._onOperate, "selectZoneByTag", data)
        end
    end)

    self:enableNodeEvents()

end

function LoginView:onOperate(cb)
    self._onOperate = function(op, ...)
        if not self._readBtn:isBright() and (op == "enterGame" or op == "openZoneList") then
            display.pushToast(L("请先同意 [腾讯游戏使用许可及服务协议] 和 [腾讯隐私政策]")) --@ignore
            return
        end
        executeFunc(cb, op, ...)
    end
end

function LoginView:setOpenListPlace(place)
    self.mOpenListPlace = place
end

function LoginView:onCleanup()
    self.movementNodes = nil
    --self:removeAccelerate()
end

function LoginView:setVersionCode(code)
    -- print("setVersionCode", code)
    if self.versionLabel then
        self.versionLabel:setString(code)
    end
end

---
--loginMenu可见性
--
function LoginView:loginMenuVisibility(visible)
    self.mLoginMenu:setVisible(visible)
end

---
--服务器列表可见性
--
function LoginView:serverListVisibility(visible)
    self._zoneView:setVisible(visible)
end

function LoginView:isZoneViewVisible()
    return self._zoneView:isVisible()
end
    

function LoginView:refreshZone(zoneData)
    if app.channelAPI:getWGPlatform() ~= 0 then
        self._curZoneLabel:setString(zoneData.name)
    else
        self._curZoneLabel:setString(string.format(L("lua_code_text_65"), zoneData.id, zoneData.name))
    end
end

function LoginView:openZoneList()
    self:loginMenuVisibility(false)
    self:serverListVisibility(true)

    self._zoneView:show()
end

function LoginView:buildPageList(pageNum,zoneCount,hasVisitedZone, hasMsdkFriend, hasRecommendZone)
    local datas = {}
    if hasVisitedZone then
        datas[#datas + 1] = {tag=0, name=L("text_10000016")}
    end

    -- 插入 好友所在服务器
    if hasMsdkFriend then
        datas[#datas + 1] = {tag=999999, name=L("lua_code_text_80")}
    end

    if hasRecommendZone then
        datas[#datas + 1] = {tag=999998, name=L("RecommendZoneStr")}
    end

    local numPerPage = self.numPerPage
    for i = 1,pageNum do
        local minServer =  math.max(zoneCount-(i*numPerPage)+1,1)
        local maxServer = zoneCount-(i-1)*numPerPage
        local server1Name = ""
        local server2Name = ""
        if minServer < 10 then
            server1Name = "00" .. minServer
        elseif minServer < 100 then
            server1Name = "0" .. minServer
        else
            server1Name = "" .. minServer
        end
        
        if maxServer < 10 then
            server2Name = "00" .. maxServer
        elseif maxServer < 100 then
            server2Name = "0" .. maxServer
        else
            server2Name = "" .. maxServer
        end
        
        local nameString = server1Name .. "--" .. server2Name

        datas[#datas + 1] = {tag=i, name=nameString}
    end
    self._zoneView:refreshList(datas)
end


function LoginView:updateZoneList(zoneDatas)
    self._zoneView:refreshZones(zoneDatas)
end


return LoginView